package wwj.controller;

import wwj.bean.User;
import wwj.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    LoginService loginService=new LoginService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String user_password = req.getParameter("user_password");
        System.out.println(username+";"+user_password);
        User user=new User(username,user_password);
        User user_final = loginService.checkLogin(user);
        System.out.println(user_final.getId()+";"+user_final.getEmail());
        resp.setContentType("text/html;charset=UTF-8");
        if(user_final.getId()==-1){
            resp.getWriter().write("系统问题");
            resp.setHeader("refresh","3;login.jsp");

        }else if(user_final.getId()==0){
            resp.getWriter().write("用户名密码错误");
            resp.setHeader("refresh","3;login.jsp");
        }else {
            req.getSession().setAttribute("user",user);
            resp.sendRedirect("HomePageServlet");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
